package com.yuanwushe.blue.video;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.yuanwushe.blue.model.VideoItem;
import com.yuanwushe.blue.permission.Permission;
import com.yuanwushe.blue.thread.IThread;
import com.yuanwushe.blue.thread.VideoThread;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    public VideoThread VideoThread;
    private ListView VideoListView;
    private VideoAdapter VideoAdapter;
    private List<VideoItem> VideoList;
    private MenuItem mRefreshMenuItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("视频列表");
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        VideoListView = findViewById(R.id.video_list);
        VideoList = new ArrayList<>();
        VideoAdapter = new VideoAdapter(this, VideoList);
        VideoListView.setAdapter(VideoAdapter);
        VideoThreadRun();
        VideoListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(MainActivity.this, VideoActivity.class);
                VideoItem item = VideoList.get(i);
                intent.setData(Uri.parse(item.path));
                startActivity(intent);
            }
        });
    }

    private void VideoThreadRun()
    {
        VideoList.clear();
        VideoThread = new VideoThread(this, new IThread<VideoItem>() {
            @Override
            public void success(final VideoItem object) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        VideoList.add(object);
                        VideoAdapter.notifyDataSetChanged();
                    }
                });
            }

            @Override
            public void cancel() {
                if (mRefreshMenuItem != null){
                    mRefreshMenuItem.setTitle(R.string.stopRefresh);
                }
            }
        });
        VideoThread.execute();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        mRefreshMenuItem = menu.findItem(R.id.menu_refresh);

        if (VideoThread != null && VideoThread.getStatus() == AsyncTask.Status.RUNNING)
        {
            mRefreshMenuItem.setTitle(R.string.stopRefresh);
        }
        else
        {
            mRefreshMenuItem.setTitle(R.string.refresh);
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.menu_refresh:
            {
                if (VideoThread != null && VideoThread.getStatus() == AsyncTask.Status.RUNNING)
                {
                    VideoThread.cancel(true);
                    VideoThread = null;
                }
                else
                {
                    VideoThreadRun();
                    if (mRefreshMenuItem != null)
                    {
                        mRefreshMenuItem.setTitle(R.string.refresh);
                    }
                }
            }
        }
        return true;
    }
}

class VideoAdapter extends ArrayAdapter<VideoItem>
{
    private Context Context;
    private List<VideoItem> Data;
    VideoAdapter(@NonNull Context context, List<VideoItem> objects) {
        super(context, R.layout.video_item, objects);
        this.Context = context;
        this.Data = objects;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView == null)
        {
            convertView = LayoutInflater.from(Context).inflate(R.layout.video_item, null);
        }
        VideoItem item = Data.get(position);

        ImageView videoImage = convertView.findViewById(R.id.video_thumb);
        videoImage.setImageBitmap(item.thumb);

        TextView videoTitle = convertView.findViewById(R.id.video_title);
        videoTitle.setText(item.name);

        TextView videoTime = convertView.findViewById(R.id.video_time);
        videoTime.setText(item.createdTime);

        return convertView;
    }

}
